package com.logistics.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.logistics.entity.SysPermission;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 权限Mapper接口
 */
@Mapper
public interface SysPermissionMapper extends BaseMapper<SysPermission> {
    
    /**
     * 根据角色ID获取权限列表
     */
    @Select("SELECT p.* FROM sys_permission p " +
            "INNER JOIN sys_role_permission rp ON p.id = rp.permission_id " +
            "WHERE rp.role_id = #{roleId} AND p.status = 1 " +
            "ORDER BY p.parent_id, p.sort_order")
    List<SysPermission> selectByRoleId(@Param("roleId") Long roleId);
    
    /**
     * 根据用户角色获取权限代码列表
     */
    @Select("SELECT DISTINCT p.permission_code FROM sys_permission p " +
            "INNER JOIN sys_role_permission rp ON p.id = rp.permission_id " +
            "INNER JOIN sys_user u ON u.role = (SELECT role_code FROM sys_role WHERE id = rp.role_id) " +
            "WHERE u.id = #{userId} AND p.status = 1")
    List<String> selectPermissionCodesByUserId(@Param("userId") Long userId);
}

